﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Metier;
using MesErreurs;

namespace Gestion_commerciale
{
    public partial class FDetail_Cde : Form
    {
        string numCommande;
        public FDetail_Cde(string numCom)
        {
            InitializeComponent();
            System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
            lvcdes.Items.Clear();
            lvcdes.View = View.Details;
            lvcdes.Columns.Add("Numéro Article", 100, HorizontalAlignment.Left);
            lvcdes.Columns.Add("Nom Article", 100, HorizontalAlignment.Left);
            lvcdes.Columns.Add("Prix unitaire", 100, HorizontalAlignment.Left);
            lvcdes.Columns.Add("Quantité commandée", 100, HorizontalAlignment.Left);
            lvcdes.Columns.Add("Montant", 100, HorizontalAlignment.Left);
            numCommande = numCom;
        }

        private void AfficherListe()
        {
            List<Article> mesArticles;
            DetailCde unDetail = new DetailCde();
            string  numArt, nomArt;
            int quantite;
            float prix, montant,total=0;
            ListViewItem lvitem_cde;

            mesArticles = unDetail.getLesArticles(numCommande);

            lvcdes.Items.Clear();
            lvcdes.Columns.Clear();
            lvcdes.View = View.Details;
            lvcdes.Columns.Add("1", "Numéro Article");
            lvcdes.Columns.Add("2", "Nom Article");
            lvcdes.Columns.Add("3", "Prix Unitaire");
            lvcdes.Columns.Add("4", "Quantité");
            lvcdes.Columns.Add("5", "Montant");
            lvcdes.FullRowSelect = true;
            try
            {
                foreach (Article c in mesArticles)
                {
                    numArt = c.NoArticle;
                    unDetail.RechercheUnDetail(numCommande, numArt);
                    nomArt = c.LibArticle;
                    quantite = unDetail.QteCommandee;
                    prix = c.PrixArticle;
                    montant = quantite * prix;
                    total += montant;
                    lvitem_cde = new ListViewItem(new string[] { numArt, nomArt, prix.ToString(), quantite.ToString(),montant.ToString()}, -1, Color.Black, Color.LightGreen, null);
                    lvcdes.Items.Add(lvitem_cde);
                }
                HT.Text += total.ToString()+" €";
                total *= (float) 1.196;
                TTC.Text += total.ToString() + " €";
                lvcdes.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
            }
            catch (MonException erreur)
            {
                throw erreur;
            }
        }

        private void FDetail_Cde_Load(object sender, EventArgs e)
        {
            AfficherListe();
        }
    }
}
